package com.dongbo.recorder.screenrecord.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;

import com.dongbo.recorder.screenrecord.utils.video.UIUtils;
import com.dongbo.recorder.screenrecord.AppLpds;

import java.util.List;

import static android.content.Context.ACTIVITY_SERVICE;

/**
 * @Description:
 * @Author: gstory0404@gmail
 * @CreateDate: 2020/5/6 11:22
 */
public class SystemUtils {

    /**
     * 判断程序是否在后台运行
     * @param context
     * @return
     */
    public static boolean isRunBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                    // 表明程序在后台运行
                    return true;
                } else {
                    return false;
                }
            }
        }
        return false;
    }

    /**
     * 判断程序是否在前台运行（当前运行的程序）
     * @return
     */
    public static boolean isRunForeground() {
        ActivityManager activityManager = (ActivityManager) AppLpds.getInstance()
                .getSystemService(Context.ACTIVITY_SERVICE);
        String packageName = AppLpds.getInstance().getPackageName();
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        if (appProcesses == null)
            return false;
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(packageName)
                    && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;// 程序运行在前台
            }
        }
        return false;
    }

    /**
     * 将应用拉回前台
     * @param activity
     * @param cl
     */
    public static void RunForeground(Activity activity,Class<?> cl){
        //获取ActivityManager
        ActivityManager mAm = (ActivityManager) activity.getSystemService(ACTIVITY_SERVICE);
        //获得当前运行的task
        List<ActivityManager.RunningTaskInfo> taskList = mAm.getRunningTasks(100);
        for (ActivityManager.RunningTaskInfo rti : taskList) {
            //找到当前应用的task，并启动task的栈顶activity，达到程序切换到前台
            if (rti.topActivity.getPackageName().equals(UIUtils.getPackageName())) {
                mAm.moveTaskToFront(rti.id, 0);
                return;
            }
        }
        //若没有找到运行的task，用户结束了task或被系统释放，则重新启动mainactivity
        Intent resultIntent = new Intent(activity, cl);
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        activity.startActivity(resultIntent);
    }
}
